<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: Participants.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Vote_Model_DbTable_Participants extends Engine_Db_Table {
	protected $_rowClass = 'Vote_Model_Participant';
	
	public function getSelect($params = array()) {
		$select = $this->select ();
		$prefix = $this->getTablePrefix ();
		
		$select->setIntegrityCheck ( false )->from ( $prefix . 'vote_participants' )->joinLeft ( $prefix . 'kowcore_rates', $prefix . 'kowcore_rates.object_id = ' . $prefix . 'vote_participants.participant_id AND ' . $prefix . 'kowcore_rates.object_type = "vote_participant"', array ('cnt' => 'COUNT(' . $prefix . 'kowcore_rates.rate_id)', 'avg' => 'AVG(' . $prefix . 'kowcore_rates.score)' ) )->joinLeft ( $prefix . 'vote_challenges', $prefix . 'vote_participants.challenge_id = ' . $prefix . 'vote_challenges.challenge_id', array () )->group ( $prefix . 'vote_participants.participant_id' );
		
		if (! empty ( $params ['has_photo'] ) && $params ['has_photo']) {
			$select->where ( $prefix . 'vote_participants.photo_id <> 0' );
		}
		
		if (! empty ( $params ['where'] )) {
			$select->where ( $params ['where'] );
		}
		
		if (! empty ( $params ['keyword'] )) {
			$select->where ( $prefix . "vote_participants.title LIKE '%{$params['keyword']}%' OR " . $prefix . "vote_participants.description LIKE '%{$params['keyword']}%' OR " );
		}
		
		if (! empty ( $params ['challenge_id'] )) {
			$select->where ( $prefix . "vote_participants.challenge_id = ?", $params ['challenge_id'] );
		}
		
		if (! empty ( $params ['type'] )) {
			$select->where ( $prefix . "vote_participants.type = ?", $params ['type'] );
		}
		
		if (! empty ( $params ['approved'] )) {
			$select->where ( $prefix . "vote_challenges.approved = ?", $params ['approved'] );
		}
		
		if (! empty ( $params ['period'] )) {
			if ($params ['period'] == 'past') {
				$select->where ( $prefix . 'vote_challenges.endtime < FROM_UNIXTIME(?)', time () );
			} elseif ($params ['period'] == 'upcoming') {
				$time = time ();
				$select->where ( $prefix . 'vote_challenges.starttime > FROM_UNIXTIME(?)', $time );
			} elseif ($params ['period'] == 'active') {
				$time = time ();
				$select->where ( $prefix . 'vote_challenges.endtime > FROM_UNIXTIME(?) AND ' . $prefix . 'vote_challenges.starttime < FROM_UNIXTIME(?)', $time, $time );
			}
		}
		
		if (! empty ( $params ['not_participated'] )) {
			$select->where ( $prefix . 'vote_participants.user_id <> ?', $params ['not_participated'] );
		}
		
		if (! empty ( $params ['not_owner'] )) {
			$select->where ( $prefix . 'vote_participants.user_id <> ?', $params ['not_owner'] );
		}
		
		if (! empty ( $params ['not_voted'] )) {
			$tmp = $this->select ()->setIntegrityCheck ( false )->from ( $prefix . 'kowcore_rates', array ('object_id' ) )->where ( 'object_type = ?', 'vote_participant' )->where ( 'user_id = ?', $params ['not_participated'] );
			
			$ids = $this->getAdapter ()->fetchCol ( $tmp );
			if (! empty ( $ids )) {
				$select->where ( $prefix . 'vote_participants.participant_id NOT IN (?)', $ids );
			}
		}
		
		if (! empty ( $params ['featured'] )) {
			$select->where ( $prefix . "vote_challenges.featured = ?", $params ['featured'] );
		}
		
		if (! empty ( $params ['user_id'] )) {
			$select->where ( $prefix . "vote_participants.user_id = ?", $params ['user_id'] );
		}
		
		if (! empty ( $params ['limit'] )) {
			$select->limit ( $params ['limit'] );
		}
		
		if (! empty ( $params ['sort'] )) {
			switch ($params ['sort']) {
				case 'newest' :
					$select->order ( $prefix . 'vote_participants.creation_date DESC' );
					break;
				case 'most-rated' :
					$select->order ( 'AVG(' . $prefix . 'kowcore_rates.score) DESC' );
					break;
				case 'most-popular' :
					$select->order ( $prefix . 'vote_participants.view_count DESC' );
					break;
				case 'most-voted' :
					$select->order ( 'COUNT(' . $prefix . 'kowcore_rates.rate_id) DESC' );
					break;
				case 'random' :
					$select->order ( 'RAND()' );
					break;
			}
		}
		
		return $select;
	}
	
	public function getPaginator($params = array()) {
		$select = $this->getSelect ( $params );
		$paginator = Zend_Paginator::factory ( $select );
		
		if (! empty ( $params ['ipp'] )) {
			$params ['ipp'] = ( int ) $params ['ipp'];
			$paginator->setItemCountPerPage ( $params ['ipp'] );
		}
		
		if (! empty ( $params ['page'] )) {
			$params ['page'] = ( int ) $params ['page'];
			$paginator->setCurrentPageNumber ( $params ['page'] );
		}
		
		return $paginator;
	}
	
	public function getParticipant(array $params) {
		return $this->fetchRow ( $this->getSelect ( $params ) );
	}
	
	public function getRandomParticipant(array $params) {
		$select = $this->select ();
		$prefix = $this->getTablePrefix ();
		
		$select->setIntegrityCheck ( false )->from ( $prefix . 'vote_participants' )->joinLeft ( $prefix . 'vote_challenges', $prefix . 'vote_participants.challenge_id = ' . $prefix . 'vote_challenges.challenge_id', array () )->group ( $prefix . 'vote_participants.participant_id' );
		
		if (! empty ( $params ['challenge_id'] )) {
			$select->where ( $prefix . "vote_participants.challenge_id = ?", $params ['challenge_id'] );
		}
		
		if (! empty ( $params ['type'] )) {
			$select->where ( $prefix . "vote_participants.type = ?", $params ['type'] );
		}
		
		if (! empty ( $params ['approved'] )) {
			$select->where ( $prefix . "vote_challenges.approved = ?", $params ['approved'] );
		}
		
		if (! empty ( $params ['period'] )) {
			if ($params ['period'] == 'past') {
				$select->where ( $prefix . 'vote_challenges.endtime < FROM_UNIXTIME(?)', time () );
			} elseif ($params ['period'] == 'upcoming') {
				$time = time ();
				$select->where ( $prefix . 'vote_challenges.starttime > FROM_UNIXTIME(?)', $time );
			} elseif ($params ['period'] == 'active') {
				$time = time ();
				$select->where ( $prefix . 'vote_challenges.endtime > FROM_UNIXTIME(?) AND ' . $prefix . 'vote_challenges.starttime < FROM_UNIXTIME(?)', $time, $time );
			}
		}
		
		if (! empty ( $params ['not_participated'] )) {
			$select->where ( $prefix . 'vote_participants.user_id <> ?', $params ['not_participated'] );
		}
		
		if (! empty ( $params ['not_owner'] )) {
			$select->where ( $prefix . 'vote_participants.user_id <> ?', $params ['not_owner'] );
		}
		
		if (! empty ( $params ['not_voted'] )) {
			$tmp = $this->select ()->setIntegrityCheck ( false )->from ( $prefix . 'kowcore_rates', array ('object_id' ) )->where ( 'object_type = ?', 'vote_participant' )->where ( 'user_id = ?', $params ['not_participated'] );
			
			$ids = $this->getAdapter ()->fetchCol ( $tmp );
			if (! empty ( $ids )) {
				$select->where ( $prefix . 'vote_participants.participant_id NOT IN (?)', $ids );
			}
		}
		
		if (! empty ( $params ['limit'] )) {
			$select->limit ( $params ['limit'] );
		}
		
		if (! empty ( $params ['sort'] )) {
			switch ($params ['sort']) {
				case 'most-rated' :
					$select->order ( 'AVG(' . $prefix . 'kowcore_rates.score) DESC' );
					break;
				case 'most-popular' :
					$select->order ( $prefix . 'vote_participants.view_count DESC' );
					break;
				case 'most-voted' :
					$select->order ( 'COUNT(' . $prefix . 'kowcore_rates.rate_id) DESC' );
					break;
				case 'random' :
					$select->order ( 'RAND()' );
					break;
			}
		}
		
		return $this->fetchRow ( $select );
	}
}